package site.koudai.cloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import site.koudai.cloud.common.Result;
import site.koudai.cloud.entity.User;
import site.koudai.cloud.service.UserService;

import java.util.UUID;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/login")
    public Result<User> login(@RequestParam String username, @RequestParam String password, HttpServletRequest request) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        User user = userService.getOne(queryWrapper);
        if (user != null) {

            user = userService.getUserInfo(user.getId());

            request.getSession().setAttribute("user", user);
            return Result.success(user);
        }
        return Result.fail("用户名或密码错误");
    }

}
